#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>

// gcc 04_memshare.c -o write -D WRITE
// gcc 04_memshare.c -o read -D READ

int main(int argc, char const *argv[])
{
    //ftok获取唯一的key值
    key_t key = ftok("/", 2101);

    //根据key值 得到内存映射标识符
    int shmid =  shmget(key, 32,IPC_CREAT|0666);

    //建立映射区
    char *str = (char *)shmat(shmid, NULL, 0);

    #ifdef WRITE
    //写操作
    strcpy(str, "hello share mem");
    # endif

    #ifdef READ
    printf("读到的数据:%s\n", str);
    #endif

    //解除映射
    shmdt(str);
    return 0;
}